package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.greattrade.*;
import com.yanqu.road.entity.activity.greattrade.config.GreatTradeEventInfo;
import com.yanqu.road.entity.activity.greattrade.config.GreatTradeGoodsInfo;
import com.yanqu.road.entity.activity.greattrade.config.GreatTradeSkillsInfo;
import com.yanqu.road.entity.activity.greattrade.config.GreatTradeTitleInfo;
import com.yanqu.road.pb.activity.GreatTradeProto;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.greattrade.GreatTradeConfig;
import com.yanqu.road.server.manger.activity.greattrade.GreatTradeMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.util.Map;

public class GreatTradeActivityPb {

    public static GreatTradeProto.GreatTradeSkillsTemp.Builder buildSkillsTemp(GreatTradeSkillsInfo skillsInfo, String language) {
        GreatTradeProto.GreatTradeSkillsTemp.Builder builder = GreatTradeProto.GreatTradeSkillsTemp.newBuilder();
        builder.setId(skillsInfo.getId());
        builder.setLevel(skillsInfo.getLevel());
        builder.setCost(skillsInfo.getCost());
        builder.addAllSkillParam(skillsInfo.getSkillParam());
        builder.setBeauty(skillsInfo.getBeauty());
        return builder;
    }

    public static GreatTradeProto.GreatTradeTitleTemp.Builder buildTitleTemp(GreatTradeTitleInfo titleInfo) {
        GreatTradeProto.GreatTradeTitleTemp.Builder builder = GreatTradeProto.GreatTradeTitleTemp.newBuilder();
        builder.setId(titleInfo.getId());
        builder.setUpgradeLimit(titleInfo.getUpgradeLimit().toString());
        builder.setUnlock(titleInfo.getUnlock());
        builder.setReward(titleInfo.getReward());
        builder.setEndReward(titleInfo.getEndReward());
        return builder;
    }

    public static GreatTradeProto.GreatTradeGoodsTemp.Builder buildGoodsTemp(GreatTradeGoodsInfo goodsInfo) {
        GreatTradeProto.GreatTradeGoodsTemp.Builder builder = GreatTradeProto.GreatTradeGoodsTemp.newBuilder();
        builder.setId(goodsInfo.getId());
        builder.setType(goodsInfo.getType());
        builder.setBasePrice(goodsInfo.getBasePrice());
        builder.setChangeWeight(goodsInfo.getChangeWeight());
        builder.setChangeParam(goodsInfo.getChangeParam());
        builder.setExParam(goodsInfo.getExParam());
        builder.setIcon(goodsInfo.getIcon());
        builder.setQuality(goodsInfo.getQuality());
        return builder;
    }

    public static GreatTradeProto.GreatTradeEventTemp.Builder buildEventTemp(GreatTradeEventInfo eventInfo, String language) {
        GreatTradeProto.GreatTradeEventTemp.Builder builder = GreatTradeProto.GreatTradeEventTemp.newBuilder();
        builder.setId(eventInfo.getId());
        builder.setType(eventInfo.getType());
        builder.setTitle(ServerLanguageMgr.getContent(eventInfo.getTitle(), language));
        builder.setReward(eventInfo.getReward());
        builder.setExParam(eventInfo.getExParam());
        builder.setEndText(ServerLanguageMgr.getContent(eventInfo.getEndText(), language));
        return builder;
    }

    public static GreatTradeProto.GreatTradeUserDataTemp.Builder buildUserTemp(UserGreatTradeData userData, GreatTradeConfig config) {
        GreatTradeProto.GreatTradeUserDataTemp.Builder builder = GreatTradeProto.GreatTradeUserDataTemp.newBuilder();
        //builder.setAreaEvent(buildAreaEvent(userData.getAreaEvent()));
        for (Map.Entry<Integer, GreatTradeAreaEvent> eventEntry : userData.getAreaEventMap().entrySet()) {
            GreatTradeAreaEvent areaEvent = eventEntry.getValue();
            builder.addAreaEvent(buildAreaEvent(areaEvent));
        }
        for (Map.Entry<Integer, GreatTradeBeautySkill> beautyEntry : userData.getBeautyMap().entrySet()) {
            builder.addBeauty(buildBeauty(beautyEntry.getValue()));
        }
        builder.setDayFreeTimes(userData.getDayFreeTimes());
        builder.setDayTradeTimes(userData.getDayTradeTimes());
        for (int i = 0; i < userData.getGoodsList().size(); i++) {
            GreatTradeGoods tradeGoods = userData.getGoodsList().get(i);
            if(tradeGoods.getNum() <= 0){
                continue;
            }
            GreatTradeProto.GreatTradeBagGoodsTemp.Builder goodsBuild = GreatTradeProto.GreatTradeBagGoodsTemp.newBuilder();
            goodsBuild.setId(tradeGoods.getId());
            goodsBuild.setNum(tradeGoods.getNum());
            goodsBuild.setPrice(tradeGoods.getPrice());
            builder.addGoods(goodsBuild);
        }
        for (Map.Entry<Integer, Integer> entry : userData.getGoodsPriceList().entrySet()) {
            builder.addGoodsPrice(buildItem(entry.getKey(), entry.getValue()));
        }
        int size = userData.getIndustryPrice().size();
        if(size > 0){
            Map<Integer, Long> map = userData.getIndustryPrice().get(size - 1);
            for (Map.Entry<Integer, Long> entry : map.entrySet()) {
                builder.addIndustryPrice(buildItem(entry.getKey(), entry.getValue()));
            }
        }
        builder.setIndustry(buildIndustry(userData.getIndustry()));
        builder.setMaxAssets(userData.getMaxAssets());
        builder.setMaxTitle(userData.getMaxTitle());

        if(userData.getTradeStatus() != null) {
            builder.setTradeFinish(userData.getTradeStatus().isTradeFinish());
            builder.setAssets(userData.getTradeStatus().getAssets());
            builder.setAge(userData.getTradeStatus().getAge());
            builder.setBirth(userData.getTradeStatus().getBirth());
            builder.setCurYear(userData.getTradeStatus().getCurYear());
            builder.setHealth(userData.getTradeStatus().getHealth());
            builder.setOutTimes(userData.getTradeStatus().getOutTimes());
            builder.setBagCap(GreatTradeMgr.getUserBagCap(userData, config));
            builder.setPrestige(userData.getTradeStatus().getPrestige());
            builder.setSilver(userData.getTradeStatus().getSilver());
            builder.setTitle(userData.getTradeStatus().getTitle());
            builder.setRandomEventId(userData.getTradeStatus().getRandomEventId());
            builder.setRisk(userData.getTradeStatus().getRisk());
            builder.setHealthTimes(userData.getTradeStatus().getHealthTimes());
            builder.setEventSelectTimes(userData.getTradeStatus().getEventSelectTimes());
            //builder.setMaxAge(GreatTradeMgr.getUserMaxAge(userData, config));
            builder.setMaxHealth(GreatTradeMgr.getUserMaxHealth(userData, config, userData.getTradeStatus().getExMaxHealth()));
            builder.setStatus(userData.getTradeStatus().getStatus());
            builder.setBuyBayCapTimes(userData.getTradeStatus().getBuyBayCapTimes());
            builder.setBanquetTimes(userData.getTradeStatus().getBanquetTimes());
        }
        for (Map.Entry<Integer, GreatTradeSaltWorks> worksEntry : userData.getSaltWorksMap().entrySet()) {
            GreatTradeProto.GreatTradeSaltWorksTemp.Builder saltWorks = buildSaltWorks(worksEntry.getValue());
            saltWorks.setReserves(GreatTradeMgr.getSaltWorksReserves(userData, worksEntry.getValue(), config));
            builder.addSaltWork(saltWorks);
        }
        builder.setTotalTradeTimes(userData.getTotalTradeTimes());
        builder.setBuyGoldToken(userData.getBuyGoldToken());
        builder.setBuySliverToken(userData.getBuySliverToken());
        return builder;
    }

    public static GreatTradeProto.GreatTradeAreaEventTemp.Builder buildAreaEvent(GreatTradeAreaEvent areaEvent){
        GreatTradeProto.GreatTradeAreaEventTemp.Builder builder = GreatTradeProto.GreatTradeAreaEventTemp.newBuilder();
        if(areaEvent != null){
            builder.setEventId(areaEvent.getEventId());
            builder.setEventType(areaEvent.getEventType());
            builder.setNpcId(areaEvent.getNpcId());
            builder.addAllSelectId(areaEvent.getSelectIdxList());
            builder.addAllHandleId(areaEvent.getSelectList());
            builder.setConsume(areaEvent.getPrice());
            builder.setAreaId(areaEvent.getAreaId());
            builder.addAllVal(areaEvent.getValList());
        }
        return builder;
    }

    public static GreatTradeProto.GreatTradeSaltWorksTemp.Builder buildSaltWorks(GreatTradeSaltWorks saltWorks){
        GreatTradeProto.GreatTradeSaltWorksTemp.Builder builder = GreatTradeProto.GreatTradeSaltWorksTemp.newBuilder();
        builder.setId(saltWorks.getId());
        builder.setMiningSpeed(saltWorks.getMiningSpeed());
        builder.setReserves(saltWorks.getReserves());
        builder.setSealOrSell(saltWorks.getSealOrSell());
        builder.setTimes(saltWorks.getTimes());
        builder.setPrice(saltWorks.getPrice());
        return builder;
    }

    public static GreatTradeProto.GreatTradeBeautySkillTemp.Builder buildBeauty(GreatTradeBeautySkill beauty){
        GreatTradeProto.GreatTradeBeautySkillTemp.Builder builder = GreatTradeProto.GreatTradeBeautySkillTemp.newBuilder();
        builder.setId(beauty.getId());
        builder.setLv(beauty.getLv());
        builder.setAddLv(beauty.getAddLv());
        return builder;
    }

    public static GreatTradeProto.GreatTradeItemValueTemp.Builder buildItem(int itemId, long val){
        return GreatTradeProto.GreatTradeItemValueTemp.newBuilder().setId(itemId).setValue(val);
    }

    public static GreatTradeProto.GreatTradeIndustryTemp.Builder buildIndustry(GreatTradeIndustry industry){
        GreatTradeProto.GreatTradeIndustryTemp.Builder builder = GreatTradeProto.GreatTradeIndustryTemp.newBuilder();
        builder.setId(industry.getId());
        builder.setPrice(industry.getPrice());
        builder.setLastCondition(industry.getLastCondition());
        builder.setShares(industry.getShares());
        builder.setStartYear(industry.getStartYear());
        return builder;
    }

    public static GreatTradeProto.GreatTradeMilepostRecordTemp.Builder buildMilepost(GreatTradeMilepostRecord record){
        GreatTradeProto.GreatTradeMilepostRecordTemp.Builder builder = GreatTradeProto.GreatTradeMilepostRecordTemp.newBuilder();
        builder.setAge(record.getAge());
        builder.setType(record.getType());
        //builder.setParams(record.getParams());
        builder.addAllParams(StringUtils.stringToStringList(record.getParams(), "\\|"));
        return builder;
    }
}
